/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.webbapps.cormac_susilo.resources;

import edu.webbapps.cormac_susilo.core.IUserModel;
import edu.webbapps.cormac_susilo.wrappers.UserWrapper;
import java.net.URI;
import java.util.List;
import java.util.NoSuchElementException;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.persistence.RollbackException;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Produces;
import javax.ws.rs.core.*;
import javax.ws.rs.core.Response.Status;

/**
 * REST Web Service for User
 *
 * @author simon, tor
 */
@RequestScoped
@Path("user")
public class UserResource {

    @Inject
    private IUserModel mgr;
    @Context
    private UriInfo context;

    public UserResource() {
    }

    @GET
    @Produces("application/json")
    public Response selectAll() {
        List<UserWrapper> users = mgr.getUsers();
        GenericEntity<List<UserWrapper>> ge = new GenericEntity<List<UserWrapper>>(users) {
        };

        return Response.ok(ge).build();
    }

    @POST // New
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces("application/json")
    public Response create(
            @FormParam("username") String username, 
            @FormParam("password") String password, 
            @FormParam("avatar") String avatar, 
            @FormParam("givenName") String Givenname, 
            @FormParam("familyName") String Familyname, 
            @FormParam("email") String email) {

        if (username == null || password == null) {
            return Response.status(Status.BAD_REQUEST).build();
        }
        if (password.isEmpty() || username.isEmpty() || password.length() > 32
                || username.length() > 32) {
            return Response.status(Status.NOT_ACCEPTABLE).build();
        }
        
        try {
            UserWrapper u = mgr.createUser(username, password, avatar, Givenname, Familyname, email);
            URI uri = context.getAbsolutePathBuilder().path(String.valueOf(u.getId())).build(u);
            return Response.created(uri).build();
            
        } catch (RollbackException e) {
            // username exists
            return Response.status(Status.CONFLICT).build();
        }
    }

    @GET
    @Path("{user_id}")
    @Produces("application/json")
    public Response select(@PathParam("user_id") long user_id) {
        try {
            UserWrapper u = mgr.getUser(user_id);
            return Response.ok(u).build();
        } catch (NoSuchElementException e) {
            return Response.noContent().build();
        }
    }

    @PUT // Update
    @Path("{user_id}")
    @Consumes({MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON})
    @Produces("application/json")
    public Response update(
            @PathParam("user_id") long user_id, 
            @FormParam("username") String username, 
            @FormParam("password") String password, 
            @FormParam("avatar") String avatar, 
            @FormParam("givenName") String Givenname, 
            @FormParam("familyName") String Familyname, 
            @FormParam("email") String email) {

        if (username == null || password == null) {
            return Response.status(Status.BAD_REQUEST).build();
        }
        if (password.isEmpty() || username.isEmpty() || password.length() > 32
                || username.length() > 32) {
            return Response.status(Status.NOT_ACCEPTABLE).build();
        }

        try {
            
            UserWrapper u = mgr.updateUser(user_id, username, password, avatar, Givenname, Familyname, email);
            return Response.ok(u).build();
            
        } catch (RollbackException e) {
            return Response.status(Status.CONFLICT).build();
        } catch (NoSuchElementException e) {
            return Response.status(Status.NOT_FOUND).build();
        }
    }

    @DELETE //remove
    @Path("{user_id}")
    public Response delete(@PathParam("user_id") long user_id) {
        
        try {
            mgr.deleteUser(user_id);
            return Response.ok().build();
            
        } catch (RollbackException e) {
            return Response.serverError().build();
        } catch (NoSuchElementException e) {
            return Response.status(Status.NOT_FOUND).build();
        }
    }
}
